package com.netty.rpc.client;

import com.netty.rpc.resp.CalcResponse;
import com.netty.rpc.resp.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

public class NettyClientHandler extends SimpleChannelInboundHandler<CalcResponse> {

    private Map<String, CompletableFuture<Response>> completableFutureMap;

    public NettyClientHandler(Map<String, CompletableFuture<Response>> completableFutureMap){
        this.completableFutureMap = completableFutureMap;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, CalcResponse resp){
        CompletableFuture<Response> future = completableFutureMap.remove("1");
        if (null != future) {
            future.complete(resp);
        } else {
            throw new IllegalStateException();
        }
    }
}
